package com.mxchip.livestarmobile.mobile.ui.dial

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.mobile.base.getString
import com.mxchip.livestarmobile.mobile.db.AppDatabase
import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.state.SystemData
import com.mxchip.livestarmobile.mobile.ui.Call
import com.mxchip.livestarmobile.mobile.ui.GroupCall
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.launch
import java.util.*

class DialViewModel(application: Application) : AndroidViewModel(application) {

    private val appDatabase: AppDatabase = (application as Appli).database

    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    private val mDialNumber: MutableLiveData<String> = MutableLiveData()

    val dialNumber: LiveData<String>
        get() = mDialNumber

    fun appendDigit(digit: String) {

        val number = mDialNumber.value

        if (number.isNullOrBlank()) {
            mDialNumber.value = digit
        } else {
            if(number.length == 16) {
                mDialNumber.value = number
            }

            else {
                mDialNumber.value = (number + digit)
            }
        }
    }

    fun removeLastDigit() {

        val number = mDialNumber.value

        if (number.isNullOrBlank()) {
            return
        }

        mDialNumber.value = number.substring(0, number.length - 1)
    }

    fun clearAllDigits() {
        mDialNumber.value = null
    }

    fun makeCall(): Call {

        val number = mDialNumber.value

        if(number.isNullOrBlank()) {
            throw Throwable("号码为空")
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if(aNumber == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if(number == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                GroupCall(number)
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }

        mDialNumber.value = null
        return call
    }

    fun createCallLog(number: String) {

        val callLog = CallLog(userId = userId)

        callLog.number = number
        callLog.type = "2"
        callLog.startTime = Date()
        callLog.isCaller = true

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.calllogDao().insertAll(callLog)
        }
    }
}